package com.jotta.client;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

import com.jotta.client.communicate.Configuration;
import com.jotta.client.utility.Utility;

public class VideoPlayer extends Activity {
	private VideoView mVideoView;
	private String fileName;
	private String filePath;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.video_view);
		mVideoView = (VideoView) findViewById(R.id.surface_view);
		// mVideoView = new VideoView(VideoPlayer.this);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		mVideoView.setMediaController(new MediaController(VideoPlayer.this));

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			fileName = extras.getString(Configuration.FILE_NAME);
			filePath = extras.getString(Configuration.FILE_PATH);
		}
		updateTitle(fileName);
		setResourceForMediaPlayer(filePath);
	}

	private void updateTitle(String fileName) {
		String app_name = Configuration.getRes().getString(R.string.app_name);
		setTitle(app_name + " - " + fileName);
	}

	private void setResourceForMediaPlayer(String filePath) {
		try {
			mVideoView
					.setMediaController(new MediaController(VideoPlayer.this));
			mVideoView.setVideoPath(filePath);
			mVideoView.start();
			mVideoView.requestFocus();
		} catch (Exception e) {
			Utility.getAlertDialog(VideoPlayer.this, Configuration.getRes(),
                    e.getMessage()).show();
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mVideoView.stopPlayback();
		mVideoView = null;
	}
}
